/*
Created By: Alex Egg
Website: http://www.alexegg.com
Version: 1.7
Date: 3/19/2007

An HTML grid control that's not the common data grid.

*/


function gridr(container)
{
   this.container=$(container);
   
   this.container.style.position='relative'; 
}


gridr.prototype.cellWidth=100;
gridr.prototype.cellHeight=100;
gridr.prototype.leftBuffer=1;
gridr.prototype.topBuffer=1;
gridr.prototype.DataSource;
gridr.prototype.dimensions;
gridr.prototype.columnCount;
gridr.prototype.cellCount;
gridr.prototype.rowCount;
gridr.prototype.margin=0;


gridr.prototype.CalculateConstraints = function() 
{
	this.dimensions=this.container.getDimensions();
    this.columnCount=Math.floor  (this.dimensions.width/this.cellWidth);
    this.cellCount=this.DataSource.length;
    this.rowCount=this.CalculateRowCount(this.cellCount, this.columnCount);
    //this.rowCount=Math.round(this.cellCount/this.columnCount);
    
}

gridr.prototype.CalculateRowCount=function(cells,columns)
{
	var base=cells/columns;
	var diff=cells%columns;
	
	if(diff>0)
	{
		return base++;
	}
	else
		return base;
}

gridr.prototype.getVersion = function() 
{
    return '1.7';
}

gridr.prototype.ApplyData=function()
{
	
	
	for(i=0;i<this.DataSource.length;i++)
	{
		
		var element=this.container.down('div.cell',i);
		element.appendChild(this.DataSource[i]);
	}	
}

gridr.prototype.GetGridCells=function(index)
{
    return this.container.childNodes;
}

gridr.prototype.BuildCells=function()
{
  
  
  //clear the grid
  this.container.innerHTML=''; 
  
  
  this.CalculateConstraints();
  
  
  for(row=0;row<this.rowCount;row++)
  {
	for(column=0;column<this.columnCount;column++)
	{
		var c=document.createElement('div');
		c.style.position='absolute';
		c.style.width=this.CellWidth();
		c.style.height=this.CellHeight();
		c.style.left=this.NextLeftCord(column);
		c.style.top=this.NextTopCord(row);
		c.className='cell';
		
		this.container.appendChild(c);
		
		
	}
  }
  
  
}

gridr.prototype.CellWidth=function()
{
	return this.cellWidth+'px';
}


gridr.prototype.CellHeight=function()
{
	return this.cellHeight+'px';
}

gridr.prototype.NextLeftCord=function(index)
{
    
	return (this.cellWidth*index)+(this.margin*index)+'px';
  
}

gridr.prototype.NextTopCord=function(index)
{
    return (this.cellHeight*index)+(this.margin*index)+'px';
}

gridr.prototype.DataBind=function(data)
{
	this.BuildCells();
    this.ApplyData();
}

gridr.prototype.DoDataind=function()
{
    
}

gridr.prototype.GetItemNodes=function()
{
    
    
}

